package com.spa.modules.gen.controller;

import java.io.*;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.spa.common.api.vo.Result;
import com.spa.common.aspect.annotation.AutoLog;
import com.spa.modules.gen.entity.GenTable;
import com.spa.modules.gen.entity.GenTableColumn;
import com.spa.modules.gen.service.GenDataBaseDictService;
import com.spa.modules.gen.service.GenTableColumnService;
import com.spa.modules.gen.service.GenTableService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.net.URLEncoder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 生成信息编辑
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/gen/table")
@Api(tags = "生成信息编辑")
public class GenController {

	@Autowired
	private GenDataBaseDictService genDataBaseDictService;

	@Autowired
	private GenTableService genTableService;

	@Autowired
	private GenTableColumnService genTableColumnService;

	/**
	 * 分页列表查询
	 * 
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@SaCheckPermission("gen:table:listByPage")
	@AutoLog(value = "用户-列表")
	@GetMapping(value = "/listByPage")
	public Result<?> queryPageList(HttpServletRequest req,
			@RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize) {
		return genTableService.queryPageList(req, page, pageSize);
	}

	/**
	 * 添加
	 *
	 * @param spaUser
	 * @return
	 */
	@AutoLog(value = "用户-添加")
	@ApiOperation(value = "用户-添加", notes = "用户-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GenTable genTable) {
		return genTableService.insert(genTable);
	}

	/**
	 * 编辑
	 *
	 * @param spaUser
	 * @return
	 */
	@AutoLog(value = "用户-编辑")
	@ApiOperation(value = "用户-编辑", notes = "用户-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GenTable genTable) {
		return genTableService.saveById(genTable);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户-通过id删除")
	@ApiOperation(value = "用户-通过id删除", notes = "用户-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		return genTableService.deleteById(id);
	}

	/**
	 * 获取物理数据表列表
	 * 
	 * @param genTable
	 * @return
	 */
	@ApiOperation("数据库全表信息")
	@GetMapping("/findTableList")
	@AutoLog(value = "数据库全表信息")
	public Result<Object> findTableList(String tableName) {
		List<GenTable> list = genDataBaseDictService.findTableList(tableName);
		return Result.resultOK(list);
	}

	/**
	 * 获取物理数据表列表
	 * 
	 * @param genTable
	 * @return
	 */
	@ApiOperation("查询数据库表字段信息")
	@GetMapping("/findTableColumnList")
	@AutoLog(value = "查询数据库表字段信息")
	public Result<Object> findTableColumnList(String tableName) {
		List<GenTableColumn> list = genDataBaseDictService.findTableColumnList(tableName);
		return Result.resultOK(list);
	}

	/**
	 * 获取物理数据表列表
	 * 
	 * @param genTable
	 * @return
	 */
	@ApiOperation("保存信息")
	@GetMapping("/save")
	@AutoLog(value = "保存信息")
	public Result<Object> save(GenTable genTable) {
		genTableService.save(genTable);
		List<GenTableColumn> genTableColumnList = genTable.getColumnList();
		genTableColumnService.saveBatch(genTable.getColumnList());
		return Result.resultOK("ok");
	}

	/**
	 * 生成代码
	 *
	 * @param spaUser
	 * @return
	 * @throws Exception
	 */
	@AutoLog(value = "生成代码")
	@ApiOperation(value = "生成代码", notes = "生成代码")
	@PostMapping(value = "/createftl")
	public Result<Object> createftl(@RequestBody GenTable genTable) throws Exception {
		// 生成随机数
		genTableService.createtfl(genTable);
		return Result.resultOK("ok");

	}

}
